package com.ts.api.http.platform.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.common.util.H5DomainUrlUtils;
import com.ts.api.http.platform.entity.apply.ApiPlatApplyLiXiangHua;
import com.ts.api.http.platform.entity.dataexchange.ApiPlatDataExchangeLiXiangHua;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.platform.entity.RespPlatformNoticeApply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5Apply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5DataExchange;
import com.ts.api.module.api.convert.platform.handler.ApiPlatformPhoneMd5ConvertHandler;

import java.math.BigDecimal;
import java.util.Map;

/**
 *  理享花
 *
 *  采量文档  https://apifox.com/apidoc/shared-6b352bad-d689-4f85-af3f-07336700bc88?pwd=xr4vKD6c
 *
 *  @author WJB
 */
@ApiPlan(productId = "6", type = ApiPlanConvertType.PLAT_PHONE_MD5)
public class ApiPlatLiXiangHuaPhoneMd5Convert implements ApiPlatformPhoneMd5ConvertHandler {

    private static final String CHANNEL_CODE = "342";
    private static final String KEY = "4342ed49fba440cc93398beee40dc5ce";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        ApiPlatDataExchangeLiXiangHua data = new ApiPlatDataExchangeLiXiangHua();
        data.setPhoneMd5(apiDataExchangeParameter.getPhoneMd5());
        data.setUserName("浩安星");
        //  年龄
        data.setAge(ClassConvertUtils.toInt(apiDataExchangeParameter.getAge()));
        //  城市名称
        data.setCity(apiDataExchangeParameter.getCity());
        //  城市编码
        data.setCityCode(apiDataExchangeParameter.getAreaCode());
        //  性别 1-男 0-女
        Integer sex = ChooseEq.create()
                .when(apiDataExchangeParameter.getSex(), "1", 0)
                .when(apiDataExchangeParameter.getSex(), "0", 1)
                .end(1);
        data.setGender(sex);
        //  芝麻分
        Integer zhima = ChooseEq.create()
                .when(apiDataExchangeParameter.getSesameCredit(), "1", 2)
                .when(apiDataExchangeParameter.getSesameCredit(), "2", 3)
                .when(apiDataExchangeParameter.getSesameCredit(), "3", 4)
                .when(apiDataExchangeParameter.getSesameCredit(), "4", 5)
                .end(2);
        data.setSesame(zhima);
        //  公积金
        Integer providentFund = ChooseEq.create()
                .when(apiDataExchangeParameter.getProvidentFund(), "1", 1)
                .when(apiDataExchangeParameter.getProvidentFund(), "2", 2)
                .when(apiDataExchangeParameter.getProvidentFund(), "3", 3)
                .when(apiDataExchangeParameter.getProvidentFund(), "4", 2)
                .end(1);
        data.setAccumulationFund(providentFund);
        //  社保
        Integer socialSecurity = ChooseEq.create()
                .when(apiDataExchangeParameter.getSocialSecurity(), "1", 1)
                .when(apiDataExchangeParameter.getSocialSecurity(), "2", 2)
                .when(apiDataExchangeParameter.getSocialSecurity(), "3", 3)
                .when(apiDataExchangeParameter.getSocialSecurity(), "4", 2)
                .end(1);
        data.setSocial(socialSecurity);
        //  学历
        Integer education = ChooseEq.create()
                .when(apiDataExchangeParameter.getEducationAttainment(), "1", 1)
                .when(apiDataExchangeParameter.getEducationAttainment(), "2", 1)
                .when(apiDataExchangeParameter.getEducationAttainment(), "3", 1)
                .when(apiDataExchangeParameter.getEducationAttainment(), "4", 2)
                .when(apiDataExchangeParameter.getEducationAttainment(), "5", 3)
                .when(apiDataExchangeParameter.getEducationAttainment(), "6", 4)
                .end();
        data.setEducation(education);
        //  商业保险
        Integer commericalInsurance = ChooseEq.create()
                .when(apiDataExchangeParameter.getInsurancePolicy(), "1", 2)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "2", 1)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "3", 3)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "4", 1)
                .end(2);
        data.setInsurance(commericalInsurance);
        //  房产
        Integer house = ChooseEq.create()
                .when(apiDataExchangeParameter.getRealEstateInfo(), "1", 1)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "2", 2)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "3", 3)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "4", 2)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "5", 2)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "6", 3)
                .end(1);
        data.setHouse(house);
        //  车产
        Integer vehicle = ChooseEq.create()
                .when(apiDataExchangeParameter.getCarInfo(), "1", 3)
                .when(apiDataExchangeParameter.getCarInfo(), "2", 1)
                .when(apiDataExchangeParameter.getCarInfo(), "3", 2)
                .when(apiDataExchangeParameter.getCarInfo(), "4", 1)
                .end(3);
        data.setCar(vehicle);
        //  职业
        Integer career = ChooseEq.create()
                .when(apiDataExchangeParameter.getCareer(), "1", 1)
                .when(apiDataExchangeParameter.getCareer(), "2", 5)
                .when(apiDataExchangeParameter.getCareer(), "3", 3)
                .when(apiDataExchangeParameter.getCareer(), "4", 2)
                .when(apiDataExchangeParameter.getCareer(), "5", 4)
                .end(1);
        data.setProfessional(career);
        //  贷款额度
        Integer money = ChooseEq.create()
                .when(apiDataExchangeParameter.getLoanAmount(), "1", 30000)
                .when(apiDataExchangeParameter.getLoanAmount(), "2", 50000)
                .when(apiDataExchangeParameter.getLoanAmount(), "3", 100000)
                .when(apiDataExchangeParameter.getLoanAmount(), "4", 200000)
                .end();
        data.setLoanAmount(money);
        //  贷款期限
        data.setLoanTerm(3);
        //  贷款用途
        Integer loanUse = ChooseEq.create()
                .when(apiDataExchangeParameter.getLoanPurpose(), "1", 5)
                .when(apiDataExchangeParameter.getLoanPurpose(), "2", 4)
                .when(apiDataExchangeParameter.getLoanPurpose(), "3", 8)
                .when(apiDataExchangeParameter.getLoanPurpose(), "4", 3)
                .when(apiDataExchangeParameter.getLoanPurpose(), "5", 8)
                .when(apiDataExchangeParameter.getLoanPurpose(), "6", 6)
                .end(8);
        data.setLoanUse(loanUse);
        //  信用卡
        Integer creditCard = ChooseEq.create()
                .when(apiDataExchangeParameter.getCreditLimit(), "1", 1)
                .when(apiDataExchangeParameter.getCreditLimit(), "2", 2)
                .when(apiDataExchangeParameter.getCreditLimit(), "3", 2)
                .when(apiDataExchangeParameter.getCreditLimit(), "4", 3)
                .when(apiDataExchangeParameter.getCreditLimit(), "5", 3)
                .when(apiDataExchangeParameter.getCreditLimit(), "6", 2)
                .end(1);
        data.setCreditCard(creditCard);

        //  花呗
        Integer huaBei = ChooseEq.create()
                .when(apiDataExchangeParameter.getHbLimit(), "1", 1)
                .when(apiDataExchangeParameter.getHbLimit(), "2", 2)
                .when(apiDataExchangeParameter.getHbLimit(), "3", 2)
                .when(apiDataExchangeParameter.getHbLimit(), "4", 3)
                .when(apiDataExchangeParameter.getHbLimit(), "5", 3)
                .end(1);
        data.setFlowers(huaBei);
        //  白条
        Integer btLimit = ChooseEq.create()
                .when(apiDataExchangeParameter.getBtLimit(), "1", 1)
                .when(apiDataExchangeParameter.getBtLimit(), "2", 2)
                .when(apiDataExchangeParameter.getBtLimit(), "3", 2)
                .when(apiDataExchangeParameter.getBtLimit(), "4", 3)
                .when(apiDataExchangeParameter.getBtLimit(), "5", 3)
                .end(1);
        data.setWhiteBar(btLimit);
        //  ip
        if (ValidParam.isNotEmpty(apiDataExchangeParameter.getIp())) {
            data.setRegisterIp(apiDataExchangeParameter.getIp());
        } else {
            data.setRegisterIp("127.0.0.1");
        }
        data.setClient("Android");
        //  学历
        Integer educationAttainment = ChooseEq.create()
                .when(apiDataExchangeParameter.getEducationAttainment(), "1", 1)
                .when(apiDataExchangeParameter.getEducationAttainment(), "2", 1)
                .when(apiDataExchangeParameter.getEducationAttainment(), "3", 1)
                .when(apiDataExchangeParameter.getEducationAttainment(), "4", 2)
                .when(apiDataExchangeParameter.getEducationAttainment(), "5", 3)
                .when(apiDataExchangeParameter.getEducationAttainment(), "6", 4)
                .end(2);
        data.setEducation(educationAttainment);
        //  资质加密
        AES aes = AES.createEcb(KEY.getBytes());
        String dataStr = aes.encryptToBase64Str(JsonUtils.toJson(data));

        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("channelNum", CHANNEL_CODE);
            newMap.put("data", dataStr);
        });
        return JsonUtils.toJson(map);
    }


    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        ApiPlatApplyLiXiangHua data = new ApiPlatApplyLiXiangHua();
        data.setPhone(apiApplyParameter.getPhone());
        data.setUserName(apiApplyParameter.getUserName());
        //  年龄
        data.setAge(ClassConvertUtils.toInt(apiApplyParameter.getAge()));
        //  城市名称
        data.setCity(apiApplyParameter.getCity());
        //  城市编码
        data.setCityCode(apiApplyParameter.getAreaCode());
        //  性别 1-男 0-女
        Integer sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "1", 0)
                .when(apiApplyParameter.getSex(), "0", 1)
                .end(1);
        data.setGender(sex);
        //  芝麻分
        Integer zhima = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", 2)
                .when(apiApplyParameter.getSesameCredit(), "2", 3)
                .when(apiApplyParameter.getSesameCredit(), "3", 4)
                .when(apiApplyParameter.getSesameCredit(), "4", 5)
                .end(2);
        data.setSesame(zhima);
        //  公积金
        Integer providentFund = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", 1)
                .when(apiApplyParameter.getProvidentFund(), "2", 2)
                .when(apiApplyParameter.getProvidentFund(), "3", 3)
                .when(apiApplyParameter.getProvidentFund(), "4", 2)
                .end(1);
        data.setAccumulationFund(providentFund);
        //  社保
        Integer socialSecurity = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", 1)
                .when(apiApplyParameter.getSocialSecurity(), "2", 2)
                .when(apiApplyParameter.getSocialSecurity(), "3", 3)
                .when(apiApplyParameter.getSocialSecurity(), "4", 2)
                .end(1);
        data.setSocial(socialSecurity);
        //  学历
        Integer education = ChooseEq.create()
                .when(apiApplyParameter.getEducationAttainment(), "1", 1)
                .when(apiApplyParameter.getEducationAttainment(), "2", 1)
                .when(apiApplyParameter.getEducationAttainment(), "3", 1)
                .when(apiApplyParameter.getEducationAttainment(), "4", 2)
                .when(apiApplyParameter.getEducationAttainment(), "5", 3)
                .when(apiApplyParameter.getEducationAttainment(), "6", 4)
                .end();
        data.setEducation(education);
        //  商业保险
        Integer commericalInsurance = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", 2)
                .when(apiApplyParameter.getInsurancePolicy(), "2", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "3", 3)
                .when(apiApplyParameter.getInsurancePolicy(), "4", 1)
                .end(2);
        data.setInsurance(commericalInsurance);
        //  房产
        Integer house = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 3)
                .when(apiApplyParameter.getRealEstateInfo(), "4", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "5", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "6", 3)
                .end(1);
        data.setHouse(house);
        //  车产
        Integer vehicle = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 3)
                .when(apiApplyParameter.getCarInfo(), "2", 1)
                .when(apiApplyParameter.getCarInfo(), "3", 2)
                .when(apiApplyParameter.getCarInfo(), "4", 1)
                .end(3);
        data.setCar(vehicle);
        //  职业
        Integer career = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", 1)
                .when(apiApplyParameter.getCareer(), "2", 5)
                .when(apiApplyParameter.getCareer(), "3", 3)
                .when(apiApplyParameter.getCareer(), "4", 2)
                .when(apiApplyParameter.getCareer(), "5", 4)
                .end(1);
        data.setProfessional(career);
        //  贷款额度
        Integer money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 30000)
                .when(apiApplyParameter.getLoanAmount(), "2", 50000)
                .when(apiApplyParameter.getLoanAmount(), "3", 100000)
                .when(apiApplyParameter.getLoanAmount(), "4", 200000)
                .end();
        data.setLoanAmount(money);
        //  贷款期限
        data.setLoanTerm(3);
        //  贷款用途
        Integer loanUse = ChooseEq.create()
                .when(apiApplyParameter.getLoanPurpose(), "1", 5)
                .when(apiApplyParameter.getLoanPurpose(), "2", 4)
                .when(apiApplyParameter.getLoanPurpose(), "3", 8)
                .when(apiApplyParameter.getLoanPurpose(), "4", 3)
                .when(apiApplyParameter.getLoanPurpose(), "5", 8)
                .when(apiApplyParameter.getLoanPurpose(), "6", 6)
                .end(8);
        data.setLoanUse(loanUse);
        //  信用卡
        Integer creditCard = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", 1)
                .when(apiApplyParameter.getCreditLimit(), "2", 2)
                .when(apiApplyParameter.getCreditLimit(), "3", 2)
                .when(apiApplyParameter.getCreditLimit(), "4", 3)
                .when(apiApplyParameter.getCreditLimit(), "5", 3)
                .when(apiApplyParameter.getCreditLimit(), "6", 2)
                .end(1);
        data.setCreditCard(creditCard);

        //  花呗
        Integer huaBei = ChooseEq.create()
                .when(apiApplyParameter.getHbLimit(), "1", 1)
                .when(apiApplyParameter.getHbLimit(), "2", 2)
                .when(apiApplyParameter.getHbLimit(), "3", 2)
                .when(apiApplyParameter.getHbLimit(), "4", 3)
                .when(apiApplyParameter.getHbLimit(), "5", 3)
                .end(1);
        data.setFlowers(huaBei);
        //  白条
        Integer btLimit = ChooseEq.create()
                .when(apiApplyParameter.getBtLimit(), "1", 1)
                .when(apiApplyParameter.getBtLimit(), "2", 2)
                .when(apiApplyParameter.getBtLimit(), "3", 2)
                .when(apiApplyParameter.getBtLimit(), "4", 3)
                .when(apiApplyParameter.getBtLimit(), "5", 3)
                .end(1);
        data.setWhiteBar(btLimit);
        //  ip
        if (ValidParam.isNotEmpty(apiApplyParameter.getIp())) {
            data.setRegisterIp(apiApplyParameter.getIp());
        } else {
            data.setRegisterIp("115.29.202.109");
        }
        data.setClient("Android");
        data.setOrderId(apiApplyParameter.dataExchangeApplyId());
        data.setProtocolUrl(H5DomainUrlUtils.createUserProtocolUrl(apiApplyParameter.getProductPlanId(), apiApplyParameter.getPhoneMd5()));
        //  学历
        Integer educationAttainment = ChooseEq.create()
                .when(apiApplyParameter.getEducationAttainment(), "1", 1)
                .when(apiApplyParameter.getEducationAttainment(), "2", 1)
                .when(apiApplyParameter.getEducationAttainment(), "3", 1)
                .when(apiApplyParameter.getEducationAttainment(), "4", 2)
                .when(apiApplyParameter.getEducationAttainment(), "5", 3)
                .when(apiApplyParameter.getEducationAttainment(), "6", 4)
                .end(2);
        data.setEducation(educationAttainment);
        //  资质加密
        AES aes = AES.createEcb(KEY.getBytes());
        String dataStr = aes.encryptToBase64Str(JsonUtils.toJson(data));

        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("channelNum", CHANNEL_CODE);
            newMap.put("data", dataStr);
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespPlatformPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        return RespPlatformPhoneMd5Apply.ofSuc();
    }

    @Override
    public RespPlatformPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }
        if(!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }

        JSONObject resData = JsonUtils.get(respJson).getJSONObject("data");

        BigDecimal price = resData.getBigDecimal("price");
        String companyName = resData.getString("companyName");
        String productName = resData.getString("productName");
        String logo = resData.getString("logo");
        String orderId = resData.getString("orderId");
        String callUrl = resData.getString("url");

        RespPlatformPhoneMd5DataExchange result = RespPlatformPhoneMd5DataExchange.ofSuc();

        result.setPrice(price);
        result.setLogoPath(logo);
        result.setDisplayName(companyName);
        result.setProductName(productName);
        result.setApplyId(orderId);
        result.setCallUrl(callUrl);
        return result;
    }

    @Override
    public RespPlatformNoticeApply parseApplyNotice(RequestVO requestVO) {
        JSONObject jsonObject = JsonUtils.get(requestVO.getBody());
        String phoneMd5 = jsonObject.getString("phoneMd5");

        RespPlatformNoticeApply applyNotice = new RespPlatformNoticeApply();
        applyNotice.setPhoneMd5(phoneMd5);
        return applyNotice;
    }

    @Override
    public Map<String, Object> respApplyNotice(ApiBizStatus apiBizStatus) {
        boolean b = apiBizStatus.hasBizSuc();

        if (b) {
            return MapUtils.newHashMap(newHashMap -> {
                newHashMap.put("code", 200);
                newHashMap.put("msg", "ok");
            });
        }
        return MapUtils.newHashMap(newHashMap -> {
            newHashMap.put("code", 500);
            newHashMap.put("msg", "操作失败");
        });
    }
}
